package com.example.eureka_client.controller;


import com.example.eureka_client.entity.Answer;
import com.example.eureka_client.httpResult.Result;
import com.example.eureka_client.httpResult.ResultGenerator;
import com.example.eureka_client.service.IAnswerService;
import com.example.eureka_client.utils.PageRequest;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 问题回答 前端控制器
 * </p>
 *
 * @author plj
 * @since 2022-03-24
 */
@Controller
@RestController
@RequestMapping("/answer")
public class AnswerController {
    @Resource
    IAnswerService iAnswerService;

    @ApiOperation(value = "获取问题的所有回答")
    @GetMapping("getAnswerByQuestionId")
    public Result getAnswerByQuestionId(@RequestParam String questionId, @RequestParam int pageNum, @RequestParam int pageSize) {
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);
        return ResultGenerator.genSuccessResult(iAnswerService.getAnswersByQuestionId(questionId, pageRequest));
    }

    @ApiOperation(value = "添加回答")
    @PostMapping("addAnswer")
    public Result addAnswer(@RequestBody Answer answer){
        if(iAnswerService.addAnswer(answer)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("添加回答失败");
    }

    @ApiOperation(value = "编辑回答(只能更改内容)")
    @PostMapping("editAnswer")
    public Result editAnswer(@RequestParam String answerId, @RequestParam String content) {
        if(iAnswerService.editAnswer(answerId, content)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("编辑回答失败");
    }

    @ApiOperation(value = "删除回答")
    @PostMapping("delAnswer")
    public Result delAnswer(@RequestParam String id){
        if(iAnswerService.delAnswer(id)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("删除回答失败");
    }

    @ApiOperation(value = "添加赞同")
    @PostMapping("addAgreeNum")
    public Result addAgreeNum(@RequestParam String answerId, @RequestParam String userId){
        if(iAnswerService.addAgreeNum(answerId, userId)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("赞同失败");
    }

    @ApiOperation(value = "添加不赞同")
    @PostMapping("addNotAgreeNum")
    public Result addNotAgreeNum(@RequestParam String answerId, @RequestParam String userId){
        if(iAnswerService.addNotAgreeNum(answerId,  userId)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("不赞同失败");
    }

    @ApiOperation(value = "添加喜欢")
    @PostMapping("addLikeNum")
    public Result addLikeNum(@RequestParam String answerId, @RequestParam String userId){
        if(iAnswerService.addLikeNum(answerId, userId)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("喜欢失败");
    }

    @ApiOperation(value = "添加收藏")
    @PostMapping("addCllectionNum")
    public Result addCllectionNum(@RequestParam String answerId, @RequestParam String userId){
        if(iAnswerService.addCllectionNum(answerId,  userId)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("收藏失败");
    }

    @ApiOperation(value = "取消赞同")
    @PostMapping("reAgreeNum")
    public Result reAgreeNum(@RequestParam String answerId, @RequestParam String userId){
        if(iAnswerService.reAgreeNum(answerId, userId)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("取消赞同失败");
    }

    @ApiOperation(value = "取消不赞同")
    @PostMapping("reNotAgreeNum")
    public Result reNotAgreeNum(@RequestParam String answerId, @RequestParam String userId){
        if(iAnswerService.reNotAgreeNum(answerId,  userId)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("取消不赞同失败");
    }

    @ApiOperation(value = "取消喜欢")
    @PostMapping("reLikeNum")
    public Result reLikeNum(@RequestParam String answerId, @RequestParam String userId){
        if(iAnswerService.reLikeNum(answerId, userId)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("取消赞同失败");
    }

    @ApiOperation(value = "取消收藏")
    @PostMapping("reCllectionNum")
    public Result reCllectionNum(@RequestParam String answerId, @RequestParam String userId){
        if(iAnswerService.reCllectionNum(answerId,  userId)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("取消收藏失败");
    }

    @ApiOperation(value = "是否收藏")
    @PostMapping("isCllection")
    public Result isCllection(@RequestParam String answerId, @RequestParam String userId){
        if(iAnswerService.cllection(answerId, userId)) return ResultGenerator.genSuccessResult(true);
        else return ResultGenerator.genSuccessResult(false);
    }

    @ApiOperation(value = "是否赞同")
    @PostMapping("isAgree")
    public Result isAgree(@RequestParam String answerId, @RequestParam String userId){
        if(iAnswerService.agree(answerId, userId)) return ResultGenerator.genSuccessResult(true);
        else return ResultGenerator.genSuccessResult(false);
    }

    @ApiOperation(value = "是否不赞同")
    @PostMapping("isNotAgree")
    public Result isNotAgree(@RequestParam String answerId, @RequestParam String userId){
        if(iAnswerService.notAgree(answerId, userId)) return ResultGenerator.genSuccessResult(true);
        else return ResultGenerator.genSuccessResult(false);
    }

    @ApiOperation(value = "是否喜欢")
    @PostMapping("isLike")
    public Result isLike(@RequestParam String answerId, @RequestParam String userId){
        if(iAnswerService.like(answerId, userId)) return ResultGenerator.genSuccessResult(true);
        else return ResultGenerator.genSuccessResult(false);
    }

}
